package com.jeremyaburns.oddio.tracker 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	
	// This class represents the clickable keyboard on the left side of the interface panel.
	internal class OTKeyboard extends Sprite
	{
		static public const CLICK :String = "otclick";
		
		
		// Holds all the keys, indexed by pitch.
		private var keys :Vector.<Sprite>;
		
		
		public function OTKeyboard() 
		{
			var i:int, j:int;
			
			keys = new Vector.<Sprite>;
			
			for( j = 0 ; j < 8 ; ++j )
				for( i = 0 ; i < 12 ; ++i )
					createKey( OTStyle.noteHeight*( 12*8 - 12*j ), i );
			
			for( i = 0 ; i < keys.length ; ++i ) addChild( keys[i] );
		}

		// Create a single key Sprite. 'octaveY' denotes the y-position of the base of the octave.
		// 'index' denotes the pitch within the octave that the key represents.
		private function createKey( octaveY:Number, index:int ) :void
		{
			var newkey :Sprite = new Sprite();
			switch( index )
			{
				// White keys.
				case 0: case 2: case 4: case 5: case 7: case 9: case 11:
					newkey.graphics.lineStyle( 2, 0x111111 );
					newkey.graphics.beginFill( 0xFFFFFF );
					newkey.graphics.drawRect( 0, -OTStyle.noteHeight, OTStyle.keyWidth, OTStyle.noteHeight );
					newkey.graphics.endFill();
					break;
				
				// Black keys.
				case 1: case 3: case 6: case 8: case 10:
					newkey.graphics.lineStyle( 2, 0x111111 );
					newkey.graphics.beginFill( 0xAAAAAA );
					newkey.graphics.drawRect( 0, -OTStyle.noteHeight, OTStyle.keyWidth, OTStyle.noteHeight );
					newkey.graphics.endFill();
					newkey.graphics.beginFill( 0x000000 );
					newkey.graphics.drawRect( 0, -OTStyle.noteHeight, OTStyle.keyWidth/2, OTStyle.noteHeight );
					newkey.graphics.endFill();
					break;					
			}
			newkey.x = 0;
			newkey.y = octaveY - index*OTStyle.noteHeight;
			newkey.addEventListener( MouseEvent.CLICK, key_click );
			newkey.buttonMode = true;
			keys.push( newkey );
		}
		
		// If one of the keys is clicked, an event is fired with the pitch of the key as a parameter.
		private function key_click( e:MouseEvent ) :void
		{
			var keyIndex:int = keys.indexOf( e.target )
			dispatchEvent( new OTEvent( CLICK, keyIndex ) );
		}
	}
}




